package com.blkj.iam.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

/**
 * 平台返回值
 *
 * @author hujingbo
 * @date 2024/11/2 11:16
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ApiResult<T> {

    private int code;
    private String msg;
    private T data;

    public ApiResult(String msg, HttpStatus httpStatus) {
        this.msg = msg;
        this.code = httpStatus.value();
    }

    // 静态方法构建成功响应
    public static <T> ApiResult<T> OK() {
        return new ApiResult<>(200, "操作成功", null);
    }

    // 静态方法构建成功响应
    public static <T> ApiResult<T> OK(T data) {
        return new ApiResult<>(200, "操作成功", data);
    }

    // 静态方法构建失败响应
    public static <T> ApiResult<T> error(String msg) {
        return new ApiResult<>(500, msg, null);
    }

    public static <T> ApiResult<T> error(int code, String msg) {
        return new ApiResult<>(code, msg, null);
    }
}
